﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO.Ports;
using System.Threading;

namespace wine
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private delegate void delegateChangeBG();
        private delegate void getNewUserDelegate();

        SerialPort mSerial = null;
        Thread mainThread = null;

        ImagePlayer leftLoopAnimationPlayer;

        NameList mNameList = null;
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded_1(object sender, RoutedEventArgs e)
        {
            //this.Left = 0.0;
            //this.Top = 0.0;
            //this.Width = System.Windows.SystemParameters.PrimaryScreenWidth;
            //this.Height = System.Windows.SystemParameters.PrimaryScreenHeight;

            mSerial = new SerialPort("COM6", 57600);
            mNameList = new NameList();
            if (this.mSerial != null) {
                try
                {
                    mSerial.Open();
                }
                catch
                {
                }
            }

            this.mainThread = new Thread(new ThreadStart(ReadSerial));
            this.mainThread.Start();

            // set left animation loop
            this.leftLoopAnimationPlayer = new ImagePlayer(this, this.LoopAnimationImg, "source/bg_frame_img", "heng", 20, 220, 30);
            this.leftLoopAnimationPlayer.Play();

            // test name index
            //this.showFadeAnimation(2);
        }


        private void changeBG()
        {
            //BitmapImage image = new BitmapImage(new Uri("C:\\bg2.jpg", UriKind.Absolute));
            //this.bg1.Source = image;
        }
        private void changeBBG()
        {
            //BitmapImage image = new BitmapImage(new Uri("C:\\bg1.jpg", UriKind.Absolute));
            //this.bg1.Source = image;
        }


        private void ReadSerial()
        {
            while (true)
            {
                if (this.mSerial != null) {
                    try
                    {
                        string s = "";
                        s = mSerial.ReadLine();
                        Console.WriteLine(s);
                        int index = mNameList.getNameIndex(s);
                        Console.WriteLine(index);
                    }
                    catch
                    {
                    }

                    // TODO: now just change background statically
                    // BillHuangg: you can just add a class and I will invoke to display the text with animation
                    // OR... >_> you can finish the rest part of this program memeda
                    // this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new delegateChangeBG(changeBG));
                    this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new getNewUserDelegate(getNewUser));
                }
            }
        }

        private void getNewUser() {
            // get user

            // animation
            this.showFadeAnimation(0);
        }

        private void showFadeAnimation(int nameIndex) {
            // set name index
            Uri uri = new Uri(@"pack://siteoforigin:,,,/source/name/name-" + nameIndex + ".png", UriKind.RelativeOrAbsolute);
            this.nameImg.Source = new BitmapImage(uri);

            // hide title bg
            this.TitleBG.Opacity = 0;
            // show name and welcome
            this.NameDisplayContainer.Opacity = 1;
            
        }

        private void Window_Closing_1(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (this.mSerial != null) {
                mSerial.Close();
            }
            if (this.mainThread != null) {
                this.mainThread.Abort();
            }
        }
    }
}
